﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2011/10/27 21:44:06
 *      Version:        1.0.1101
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using Sector7.Framework.Enums;
using Sector7.Framework.Core;
using Sector7.Framework.Consts;

namespace Sector7.Framework.Utility
{
    /// <summary>
    /// 应用程序工具类
    /// </summary>
    public class ApplicationUtil
    {
        #region Public functions
        /// <summary>
        /// 获取应用入口程序集
        /// </summary>
        /// <returns></returns>
        public static Assembly GetEntryAssembly()
        {
            return Assembly.GetEntryAssembly();
        }

        /// <summary>
        /// 获取应用入口程序集路径（包含程序名）
        /// </summary>
        /// <returns></returns>
        public static string GetEntryAssemblyLocation()
        {
            return GetEntryAssembly().Location;
        }

        /// <summary>
        /// 获取应用程序版本信息
        /// </summary>
        /// <returns></returns>
        public static string GetEntryAssemblyVersion()
        {
            return GetEntryAssembly().GetName().Version.ToString(4);
        }

        /// <summary>
        /// 获取当前进程号
        /// </summary>
        /// <returns></returns>
        public static int GetCurrentProcessId()
        {
            return Process.GetCurrentProcess().Id;
        }

        /// <summary>
        /// 获取当前进程名称
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentProcessName()
        {
            return Process.GetCurrentProcess().ProcessName;
        }

        /// <summary>
        /// 获取当前应用友好名称
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentApplicationName()
        {
            return AppDomain.CurrentDomain.FriendlyName;
        }

        /// <summary>
        /// 获取当前应用基目录
        /// </summary>
        /// <returns></returns>
        public static string GetBaseDirectory()
        {
            return StringUtil.PadEnd(AppDomain.CurrentDomain.BaseDirectory, "\\");
        }

        /// <summary>
        /// 获取指定进程
        /// </summary>
        /// <param name="processName">进程名</param>
        /// <returns></returns>
        public static Process[] GetProcesses(string processName)
        {
            return Process.GetProcessesByName(processName);
        }

        /// <summary>
        /// 启动进程
        /// </summary>
        /// <param name="fileName">应用程序或文档名</param>
        /// <param name="workPath">要启动进程的工作目录</param>
        /// <param name="arguments">启动参数</param>
        /// <param name="isCheckSingleton">是否检查单例</param>
        /// <returns></returns>
        public static Process StartProcess(string fileName, string workPath, string arguments, bool isCheckSingleton)
        {
            if (isCheckSingleton)
            {
                // 1. 获取文件全名
                string processName = fileName.Substring(fileName.LastIndexOf("\\") + 1);

                // 2. 获取文件名
                processName = processName.Substring(0, processName.LastIndexOf("."));

                if (GetProcesses(processName).Length > 0)
                    throw new Exception("已经启动过指定程序。");
            }

            Process process = null;

            ProcessStartInfo processInfo = new ProcessStartInfo();
            processInfo.FileName = fileName;
            processInfo.WorkingDirectory = workPath;
            processInfo.Arguments = arguments;

            try
            {
                //启动外部程序
                process = Process.Start(processInfo);
                Log.WriteLine(LogSeverityType.INF, C_ModuleName.FRAMEWORK, "Startup {0} Successful!", fileName);

                return process;
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.WRN, C_ModuleName.FRAMEWORK, e);
                throw e;
            }
        }

        /// <summary>
        /// 解析应用程序启动参数
        /// </summary>
        /// <param name="startUpArgs">启动参数字符串</param>
        /// <returns></returns>
        public static Dictionary<string, string> ResolveStartUpArgs(string[] startUpArgs)
        {
            Dictionary<string, string> args = null;

            if (startUpArgs.Length > 0)
            {
                args = new Dictionary<string, string>();

                string[] items;

                foreach (string startUpArg in startUpArgs)
                {
                    items = startUpArg.Split('=');
                    args.Add(items[0], items[1]);
                }
            }

            return args;
        }

        /// <summary>
        /// 解析命令行参数
        /// </summary>
        /// <param name="commond"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ResolveCommondLine(string commond)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
